package com.bsj.power.pls.protocol;

import com.bsj.power.common.def.vo.pls.uwb.UWBInfoVO;
import com.bsj.tools.Conv;

import java.util.concurrent.atomic.AtomicInteger;

public class UWBPacket {

    //dataLen固定长度
    static int MIN_LEN = 1 + 1 + 2 + 2 + 2 + 4 + 8;

    private static AtomicInteger snNum = new AtomicInteger(0);

    public static int suNum() {
        int i = snNum.incrementAndGet();
        if (i > 9999999) {
            snNum = new AtomicInteger(0);
        }
        return i;
    }

    public static byte[] comboData(UWBInfoVO uwbInfoVO) {
        int offset = 0;
        byte[] context = uwbInfoVO.getContext();
        int length = context.length;
        int len = 1 + 1 + 2 + MIN_LEN + length + 2;
        byte[] result = new byte[len];
        byte[] head = uwbInfoVO.getHead();
        //标识 55H
        result[offset] = head[offset];
        offset = offset + 1;
        //标识 AAH
        result[offset] = head[offset];
        offset = offset + 1;
        //数据长度 2字节
        int dataLen = MIN_LEN + length;
        Conv.setShortNetOrder(result, offset, dataLen);
        offset = offset + 2;
        //节点编号
        result[offset] = (byte) uwbInfoVO.getNodeNo();
        offset = offset + 1;
        //数据包装类型
        result[offset] = (byte) uwbInfoVO.getDataType();
        offset = offset + 1;
        //备用
        offset = offset + 2;
        //不分包 为0时默认不分包
        Conv.setShortNetOrder(result, offset, 0);
        offset = offset + 2;
        //分包信息
        Conv.setShortNetOrder(result, offset, 0);
        offset = offset + 2;
        //报文流水号
        Conv.setIntNetOrder(result, offset, suNum());
        offset = offset + 4;
        //报文时间信息
        Conv.setLongNetOrder(result, offset, uwbInfoVO.getTime());
        offset = offset + 8;
        //数据内容
        System.arraycopy(context, 0, result, offset, length);
        offset = offset + length;
        //包校验
        Conv.setShortNetOrder(result, offset, 0);
        offset = offset + 2;
        return result;
    }

    public static UWBInfoVO packet(byte[] data) {
        int offset = 0;
        byte one = data[offset];
        offset = offset + 1;
        byte two = data[offset];
        offset += 1;
        int length = data.length;
        if (one == 0x55 && two == (byte) 0xAA) {
            UWBInfoVO uwbInfoVO = new UWBInfoVO();
            int dataLen = Conv.getShortNetOrder(data, offset);
            offset = offset + 2;
            if (offset + dataLen + 2 < length) {
                return null;
            }
            uwbInfoVO.setDataLen(dataLen);
            int nodeNo = Byte.toUnsignedInt(data[offset]);
            uwbInfoVO.setNodeNo(nodeNo);
            offset = offset + 1;
            int dataType = Byte.toUnsignedInt(data[offset]);
            uwbInfoVO.setDataType(dataType);
            offset = offset + 1;
            //备用
            byte[] spare = new byte[2];
            spare[0] = data[offset];
            offset = offset + 1;
            spare[1] = data[offset];
            offset = offset + 1;
            uwbInfoVO.setSpare(spare);
            //总包数
            int totalPacket = Conv.getShortNetOrder(data, offset);
            uwbInfoVO.setTotalPacket(totalPacket);
            offset = offset + 2;
            int nowPacket = Conv.getShortNetOrder(data, offset);
            uwbInfoVO.setNowPacket(nowPacket);
            offset = offset + 2;
            int serial = Conv.getIntNetOrder(data, offset);
            uwbInfoVO.setSerial(serial);
            offset = offset + 4;
            long time = Conv.getLongNetOrder(data, offset);
            offset = offset + 8;
            uwbInfoVO.setTime(time);
            int contextLen = dataLen - MIN_LEN;
            byte[] context = new byte[contextLen];
            System.arraycopy(data, offset, context, 0, contextLen);
            uwbInfoVO.setContext(context);
            return uwbInfoVO;
        }
        return null;
    }

}
